package es.ats.codington.Spring.Controllers;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.EventServiceImpl;

import es.ats.codington.festival.services.VisitorServiceImpl;




/**
 * The Class AdminController.
 *
 * @author 5Cs
 */
@Controller
public class AdminController {
	
	static int idEvent;
	/**
	 * Actual arrays to send jsp.
	 *
	 * @param request the request
	 * @return the model and view
	 */
	/**
	 * Actual arrays to send jsp.
	 *
	 * @param request the request
	 * @return the model and view
	 */
	@RequestMapping(value = "/eventAdmin.htm")
	public ModelAndView actualArraysToSendJsp(HttpServletRequest request){
		 Visitor visitor = (Visitor) request.getSession().getAttribute("visitor");
		 int IdVisitor = visitor.getVisitorId();
		 ModelAndView model = new ModelAndView();
		 try{		
				/**
				 * Your Events
				 */
			 	EventServiceImpl eve = new EventServiceImpl();
				ArrayList<Event> event = new ArrayList<Event>();				
				event = eve.obtainAllEvent();

				
				
				model.addObject("AllEvents", event);
				model.setViewName("listEventAdmin");
			
					
		    }catch(Exception theException){
		        System.out.println(theException); 
		   }
		return model;
					
	}
	
	/**
	 * List visitors.
	 *
	 * @param request the request
	 * @return the model and view
	 * @throws SQLException the SQL exception
	 * @throws ClassNotFoundException the class not found exception
	 */
	@RequestMapping(value = "/listVisitorAdmin.htm")
	public ModelAndView listVisitors(HttpServletRequest request) throws SQLException, ClassNotFoundException{		
		Visitor admin = (Visitor) request.getSession().getAttribute("visitor");
		int idAdmin = admin.getVisitorId(); 
		VisitorServiceImpl vis = new VisitorServiceImpl();		
		ModelAndView model = new ModelAndView();
		model.addObject("Visitors", vis.listAllVisitors(idAdmin));
		model.setViewName("ListVisitors");
		 
		 return model;
	}
	
	/**
	 * Delete visitor.
	 *
	 * @param request the request
	 * @return the string
	 * @throws Exception the exception
	 */
	@RequestMapping(value = "/deleteVisitor.htm")
	public String deleteVisitor(HttpServletRequest request) throws Exception{
		int idVisitor = Integer.parseInt(request.getParameter("visitorID"));
		VisitorServiceImpl vis = new VisitorServiceImpl();
		vis.deleteVisitor(idVisitor);
		return ("redirect:/listVisitorAdmin.htm");
	}	
	
	@RequestMapping(value = "/deleteEventAdmin.htm")
	public String deleteEvent(HttpServletRequest request){	
		 HttpSession session = request.getSession(true);
		 Visitor visitor = (Visitor) request.getSession().getAttribute("visitor");
		 int IdVisitor = visitor.getVisitorId();
		 int idEvent = Integer.parseInt(request.getParameter("IdEvento"));
		 
		 ModelAndView model = new ModelAndView();

		 try{					 
			 	EventServiceImpl eve = new EventServiceImpl();
				//ArrayList<Event> event = new ArrayList<Event>();				

				int i = eve.deleteEvent(idEvent);
				return "redirect:/eventAdmin.htm";


		 }catch(Exception theException){
		        System.out.println(theException); 
				return "redirect:/eventAdmin.htm";
		   }

					
	}

	
	@RequestMapping(value = "/updateSelectEvent.htm")
	public ModelAndView updateSelectEvent(HttpServletRequest request){
		
		 HttpSession session = request.getSession(true);
		idEvent = Integer.parseInt(request.getParameter("Id"));
		
		
	 //desde GET
		 ModelAndView model = new ModelAndView();
		 try{		
			 	EventServiceImpl eve = new EventServiceImpl();			
				Event event = eve.updateSelectEvent(idEvent);
					
				model.addObject("Event", event);
				model.setViewName("updateEvent");
			
					

				//return "redirect:/eventAdmin.htm";					

		    }catch(Exception theException){
		        System.out.println(theException); 
		   }
		return model;
					
				
	}
	
	@RequestMapping(value = "/updateEventServlet.htm")
	public String updateEvent(HttpServletRequest request){
		
		 //desde POST
			try{

				
				
				String evento = request.getParameter("evento");	
				String description = request.getParameter("description");
				String type  = request.getParameter("type");
						
				int duration = Integer.parseInt(request.getParameter("duration"));
				int place = Integer.parseInt(request.getParameter("place"));
				
				int capacity = Integer.parseInt(request.getParameter("capacity"));
				
	            Event event = new Event();
	            	
	            event.setName(evento);
	            event.setDescription(description);
	            event.setTypeEvent(type);
	            event.setIdPlace(place);
	            event.setDuration(duration);
	            event.setCapacity(capacity);
	            event.setId(idEvent);
				
	            
			 	EventServiceImpl eve = new EventServiceImpl();			
				int resul = eve.updateEvent(event);
				
				if(resul == 0){
					request.setAttribute("error", "Update Info incorrect.");
					return "updateEvent";
				}		

				else{
					request.setAttribute("ok", "Profile update succesfully");
					/*int admin = (Integer) request.getAttribute("admin");*/
					return "redirect:/eventAdmin.htm";
					
				}

	           
	        }catch(Exception theException){
	        	request.setAttribute("error", "Update Infor incorrect."+ theException.getMessage());
				return "updateEvent";
	        }
	}
}
